package com.example.admin.yuejian21.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.admin.yuejian21.Bean.Login;
import com.example.admin.yuejian21.Model.UserModel;
import com.example.admin.yuejian21.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {
    private Button reg,login;
    private EditText et_sjh,et_password;
  public static String userid;
    public static String username;
    public static Object photo;
    public static String sessionid;
    private String openidString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
    }
    public void initViews(){

        login = (Button) findViewById(R.id.login);
        reg = (Button) findViewById(R.id.reg);
        et_sjh = (EditText) findViewById(R.id.sjh);
        et_password = (EditText) findViewById(R.id.password);

    }

    public void initEvents() {
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,RegActivity.class);
                startActivityForResult(intent,1);
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //拿到用户输入的用户名、密码.
                String tel = et_sjh.getText().toString();
                String password = et_password.getText().toString();

                //1. 定义retrofit对象
               /* Retrofit retrofit = new Retrofit.Builder()
                        .addConverterFactory(GsonConverterFactory.create())
                        .baseUrl("http://172.24.10.130:8080/")
                        .build();*/
                //2. 创建访问API请求
                /*UserService service = retrofit.create(UserService.class);
                Call<User> call = service.getLogin(name, pwd);*/
                UserModel userModel = new UserModel();
                Call<Login> call = userModel.login(tel,password);
                //3. 发送请求
                call.enqueue(new Callback<Login>(){

                    @Override
                    public void onFailure(Call<Login> arg0, Throwable arg1) {
                        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Call<Login> user, Response<Login> response) {
                        String tel = et_sjh.getText().toString();
                        String password = et_password.getText().toString();

                        if(tel.equals("") || password.equals("")){
                            Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                        }
                        else {
                            userid=response.body().getId();
                            username=response.body().getUser_name();
                            photo=response.body().getPhoto();
                            sessionid=response.body().getSessionid();

                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            saveUser();

                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);                                //传用户Id
                            startActivity(intent);
                            finish();

                        }
                    }

                });
            }
        });
    }


    public void saveUser(){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putString("tel",et_sjh.getText().toString());



        editor.putString("password",et_password.getText().toString());
        editor.commit();//提交修改
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1 && resultCode==RESULT_OK){
            String username = data.getStringExtra("tel");
            et_sjh.setText(username);
        }

    }
}
